package app.utils;

import app.system.entity.SysComment;
import app.system.entity.SysMusic;
import app.system.entity.SysMusicList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 网络爬虫
 */
public class crawler {
    public static SysMusicList getmusiclist(){
        String WangYiYun_MusicList = "https://music.163.com/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset=1120";
        String re_title = "a title=\"(.*?)\" hre";
        String re_href = "href=\"(.*?)\" clas";
        String re_bofang = "class=\"nb\">(.*?)</span> ";
        try {
                Connection connection = Jsoup.connect(WangYiYun_MusicList);
            Map<String, String> header = new HashMap<String, String>();
            header.put(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            header.put("Accept-Encoding", "gzip, deflate");
            header.put("Accept-Language", "zh-CN,zh;q=0.9");
            header.put("Cache-Control", "max-age=0");
            header.put("Connection", "close");
            header.put(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64;X11; Linux 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            Document document = connection.ignoreContentType(true).headers(header).get();
            Elements divs = document.getElementsByClass("u-cover u-cover-1");
            for (Element div:divs){
                System.out.println(div.toString());
                System.out.println(div.getElementsByAttribute("src"));
                String img = div.getElementsByAttribute("src").toString();
                String list_photourl = img.substring(25,img.length()-2);
                System.out.println(list_photourl);
                String aTage = div.getElementsByTag("a").toString();
                Pattern pattern = Pattern.compile(re_bofang);
                Matcher matcher = pattern.matcher(div.toString());
                if (matcher.find()){
                    String list_sum1 = matcher.group();
                    String list_sum = list_sum1.substring(11,list_sum1.length()-7);
                    System.out.println(list_sum);

                    Pattern pattern1 = Pattern.compile(re_title);
                    Matcher matcher1 = pattern1.matcher(aTage);
                    if (matcher1.find()){
                        String list_name1 = matcher1.group();
                        String list_name = list_name1.substring(9,list_name1.length()-6);
                        System.out.println(list_name);
                        String list_note = list_name1.substring(9,list_name1.length()-6);;
                        Pattern pattern2 = Pattern.compile(re_href);
                        Matcher matcher2 = pattern2.matcher(aTage);
                        if (matcher2.find()){
                            String href1 = matcher2.group();
                            String href ="https://music.163.com/#"+href1.substring(6,href1.length()-6);
                            System.out.println(href);
                        }
                    }
                }

            }

        }catch (IOException e){
            System.out.println(e);
        }
        return null;
    }
    public static SysComment getComment(){
        try {
            String href = "https://music.163.com/#/playlist?id=7275750747";
            Connection connection = Jsoup.connect(href);
            Map<String, String> header = new HashMap<String, String>();
            header.put(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            header.put("Accept-Encoding", "gzip, deflate");
            header.put("Accept-Language", "zh-CN,zh;q=0.9");
            header.put("Cache-Control", "max-age=0");
            header.put("Connection", "close");
            header.put(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64;X11; Linux 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            Document document = connection.ignoreContentType(true).headers(header).get();
            System.out.println(document.toString());
        }catch (IOException e){
            System.out.println(e);
        }
        return null;
    }

    public static void getmusin(){
        try {
            String href = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token=";
            Connection connection = Jsoup.connect(href);
            Map<String, String> header = new HashMap<String, String>();
            header.put(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            header.put("Accept-Encoding", "gzip, deflate");
            header.put("Accept-Language", "zh-CN,zh;q=0.9");
            header.put("Cache-Control", "max-age=0");
            header.put("Connection", "close");
            header.put(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64;X11; Linux 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            Document document = connection.ignoreContentType(true).headers(header).post();
            System.out.println(document.toString());
        }catch (IOException e){
            System.out.println(e);
        }
    }

    public void getmusic(){
        try {
            String href = "https://music.163.com/weapi/cloudsearch/get/web?csrf_token=";
            Connection connection = Jsoup.connect(href);
            Map<String, String> header = new HashMap<String, String>();
            header.put(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            header.put("Accept-Encoding", "gzip, deflate");
            header.put("Accept-Language", "zh-CN,zh;q=0.9");
            header.put("Cache-Control", "max-age=0");
            header.put("Connection", "close");
            header.put(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64;X11; Linux 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            connection.data("params","4Ks33ZjYPDF5TVbtSr4/S6cKCL3HU1ozwuzJ7bV+2xsipMNrm3eiIxxM5y34qQE/t0zcc3G7EZofZS8BGqlZdYHUKNv5CkEDSh2zOHf+iZ7wUbwxflL6UKHhIbQZ6Ro6CJY7CQe+CNCs62KzW8JqiOphQOHIRY/5HSttQ6uVk+G6XQO7V881p4uCHX7DQRUIinJKZ12NCa9XEspQVtN0KwRPbWtHdK/2Dv305aLcDyJdFsIz4axyyx5wFikQjwvqnVuINgfVaXnDoOarR1HfZby2HNNwkZLnLnD5W3qKnWbRAHWADigXGGym2+4soLZ0");
            connection.data("encSecKey","b657f5c37211d2ac26d1d0a32b179688a64612dab747ada8c78cbca2705b46b84b11551d3b9f74b496a668073b3e164b0e700eb8f793633c88f3a1b513c4c5fdd4efd345fba6ed1bae2ebe900b17870f875d9f7c12aceeb9df15c988213985a638929212af179fa57a549ccf071e3fd2af1573d4ad7db0a0a52749cd21be3833");
            Document document = connection.ignoreContentType(true).headers(header).post();
            String reponse = document.getElementsByTag("body").toString();
            System.out.println(reponse.substring(6,reponse.length()-7));
            JSONObject jsonObject = JSON.parseObject(reponse.substring(6,reponse.length()-7));
            JSONObject jsonObject1 = (JSONObject) jsonObject.get("result");
            JSONArray jsonArray = (JSONArray) jsonObject1.get("songs");
            for (int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject2 = (JSONObject)  jsonArray.get(i);
                System.out.println(JSONObject.toJSONString(getData(jsonObject2)));

            }
        }catch (IOException e){
            System.out.println(e);
        }
    }

    public static SysMusic getData(JSONObject jsonObject2){
        SysMusic sysMusic = new SysMusic();
        String MusicName = jsonObject2.getString("name");
        int MusinId = jsonObject2.getInteger("id");
        JSONObject jsonObjectphotoUrl = (JSONObject) jsonObject2.get("al") ;
        JSONArray jsonArray = (JSONArray)jsonObject2.get("ar");
        JSONObject jsonObjectAuther = (JSONObject) jsonArray.get(0);
        String auther = jsonObjectAuther.getString("name");
        String picUrl = jsonObjectphotoUrl.getString("picUrl");
        sysMusic.setMusic_phoyourl(picUrl);
        sysMusic.setMusic_author(auther);
        sysMusic.setMusic_id(String.valueOf(MusinId));
        sysMusic.setMusic_name(MusicName);
        sysMusic.setMusic_uploadauthorid(String.valueOf(99999));
        sysMusic.setMusic_url("https://music.163.com/song/media/outer/url?id="+MusinId+".mp3");
        return sysMusic;
    }

    public static void getCommentV2(){

    }
}
